package com.coocaa.typhoon.api.v1.userfile.util;

import lombok.experimental.UtilityClass;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bijiahao
 * @date : 2025/9/19 19:55
 * @description
 */
@UtilityClass
public class PdfUtil {
    private static final int DPI = 150;               // 清晰度，150 足够预览
    private static final Float COMPRESS_QUALITY = 0.85f;   // 图片质量 85%

    public static List<String> convertAndUpload(InputStream pdfStream) throws IOException {
        List<String> urlList = new ArrayList<>();
        try (PDDocument doc = PDDocument.load(pdfStream)) {
            PDFRenderer renderer = new PDFRenderer(doc);
            for (int i = 0; i < doc.getNumberOfPages(); i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, DPI, ImageType.RGB);
                Thumbnails.of(image)
                        .scale(1.0)              // 不缩放
                        .outputQuality(COMPRESS_QUALITY)
                        .outputFormat("png")
                        .toFile(new File("/Users/bijiahao/test/output_" + i + ".png"));
            }
        }
        return urlList;
    }
}
